﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14
import QtGraphicalEffects 1.0
import "./components/"
import "./exportReport"
import "./animation"
import "./licenseManager/"
import "./barCode"

ApplicationWindow {
    id: mainwindow
    visible: true


    title: qsTr("靶材核素分离软件")
    color: "transparent"
    // 先把全局变量放这里了
    width: 1920*dp
    height: 1250*dp
    Config{
        id: config
    }
    Component.onCompleted: {
        mainwindow.height = Math.min(mainwindow.height, Screen.height)
        mainwindow.width = Math.min(mainwindow.width, Screen.width)
    }

    property int expStatus: 0 //0空闲 1实验中 2 暂停
    property int flagss: 0
    property bool isWindowMaximized: mainwindow.visibility===Window.Maximized     // 原为flagzz
    property int flagcc: 0
    property int flagtab: 0         // 0 详细版 1 简化版
    // 记录实验开始时间（初始化为0，表示未开始）
    property var startTime: 0
    // 计算实验结束时间（当前时间加上10分钟对应的毫秒数）
    property var endTime: 0
    // 用于控制定时器是否运行的标志
    property bool timerRunning: false

    property var name:Qt.formatDateTime(new Date, "yyyyMMddhhmmss")
    property var expDate:Qt.formatDateTime(new Date, "yyyy-MM-dd")
    property var operator: ""
    property var expSettingName: ""
    property var expSettingIndex: 0
    property var expConsumblesTypeName: ""
    property var expConsumblesTypeIndex: 0
    property var barCode: ""
    property var memo: ""

    property var barCodeIsValid: false

    property bool gConnectionState: true        // 通信状态
    property bool gSpeechState: true            // 语义控制状态
    minimumHeight: 960*dp
    minimumWidth: 1600*dp

    BoxCheckConsumablesType{
        id:startinfo
        visible: true
        onClosing:{
            expConsumblesType.setIndex(expConsumblesTypeIndex)
            expConfigName.model=GetMessageInterface.getExperimentNameByConsumables(expConsumblesType.getText())
            expConfigName.setText(expSettingName)
            var data = GetMessageInterface.getExperimentLog(expConsumblesType.getText(),expConfigName.getText());
            experimentLog.setData(data)
        }
    }



    function updateReportInfo(){    // 更新打印报告信息
        CReport.updateExperimentInfo({
                                         "ConsumableName": expConsumblesType.getText(),
                                         "MethodName": expConfigName.getText(),
                                         "ExperimentName": expInfoName.getText(),
                                         "EquipmentName": "BK-RadPharm-001",
                                         "ConsumableCode": edit_barCode.getText(),
                                         "ExecDate": sttime.getText(),
                                         "Operator": edit_operator.getText(),
                                         "Memo": edit_memo.getText()})
    }

    onExpStatusChanged: {   // 实验状态监听
        if(expStatus == 0 || expStatus == 2){
            CAnimation.changeDetector(false)
        }else if(expStatus == 1){
            CAnimation.changeDetector(true)
        }
    }

    // 阴影
    DropShadow {
        anchors.fill: mainwindow_rect
        radius: 12*dp
        samples: 22
        source: mainwindow_rect
        color: Qt.rgba(0, 0, 0, 0.2)
    }
    //    // 四周边框调整尺寸   (原自定义标题栏功能) (别删了我好不容易算的万一哪天再改回来)
    //    Item{
    //        visible: !isWindowMaximized
    //        z: 1
    //        anchors.fill: parent
    //        // 左边框
    //        MouseArea{
    //            height: mainwindow_rect.height; width: 8*dp
    //            x: mainwindow_rect.x - width
    //            y: mainwindow_rect.y
    //            cursorShape: Qt.SizeHorCursor
    //            property int originXX
    //            onPressed: {
    //                originXX = parent.width - mouseX     // 相对于右边的坐标
    //            }onPositionChanged: {
    //                var mouseXX = parent.width - mouseX
    //                if(Math.abs(originXX-mouseXX)<1) return
    //                mainwindow.x -= (mouseXX - originXX)
    //                mainwindow.width += (mouseXX - originXX)
    //                originXX = mouseXX
    //            }
    //        }
    //        // 右边框
    //        MouseArea{
    //            height: mainwindow_rect.height; width: 8*dp
    //            x: mainwindow_rect.x + mainwindow_rect.width
    //            y: mainwindow_rect.y
    //            cursorShape: Qt.SizeHorCursor
    //            property int originX
    //            onPressed: {
    //                originX = mouseX
    //            }onPositionChanged: {
    //                mainwindow.width += (mouseX - originX)
    //            }
    //        }
    //        // 上边框
    //        MouseArea{
    //            width: mainwindow_rect.width; height: 8*dp
    //            x: mainwindow_rect.x
    //            y: mainwindow_rect.y - height
    //            cursorShape: Qt.SizeVerCursor
    //            property int originYY
    //            onPressed: {
    //                originYY = parent.height - mouseY     // 相对于右边的坐标
    //            }onPositionChanged: {
    //                var mouseYY = parent.height - mouseY
    //                if(Math.abs(originYY-mouseYY)<1) return
    //                mainwindow.y -= (mouseYY - originYY)
    //                mainwindow.height += (mouseYY - originYY)
    //                originYY = mouseYY
    //            }
    //        }
    //        // 下边框
    //        MouseArea{
    //            width: mainwindow_rect.width; height: 8*dp
    //            x: mainwindow_rect.x
    //            y: mainwindow_rect.y + mainwindow_rect.height
    //            cursorShape: Qt.SizeVerCursor
    //            property int originY
    //            onPressed: {
    //                originY = mouseY
    //            }onPositionChanged: {
    //                mainwindow.height += (mouseY - originY)
    //            }
    //        }

    //    }


    Timer {
        id:dsq
        interval: 1000
        running: true
        repeat: true
        onTriggered: {
            if(timerRunning===true){
                endTime = endTime-1;
                if (endTime <= 0) {
                    endTime = 0;
                }
            }
        }
    }
    function formatTime(time) {
        var hours=  Math.floor(time/3600)
        var minutes = Math.floor((time-hours*3600) / 60)
        var seconds = time-hours*3600-minutes*60
        return String(hours).padStart(2, '0')+":"+String(minutes).padStart(2, '0') + ":" + String(seconds).padStart(2, '0');
    }

    property real dp: config.dp
    //    flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint

    // 内窗口（外面那个Windows用于显示窗口周围阴影以及拖拽缩放功能）
    Rectangle{
        id: mainwindow_rect
        //        x: isWindowMaximized?0:15*dp; y: isWindowMaximized?0:15*dp
        radius: 6*dp
        border.color: Qt.rgba(0, 0, 0, 0.1)
        //        width: mainwindow.width - x*2
        //        height: mainwindow.height - y*2
        anchors.fill: parent

        //        // 标题栏
        //        MainTitle{
        //            id: customTitleBar
        //        }
        // 内容(主要布局)
        Rectangle{
            id: mainLayout
            //            radius: 6*dp
            //            y: customTitleBar.height
            //            x: 1
            //            height: mainwindow_rect.height-customTitleBar.height - 1
            //            width: mainwindow_rect.width - 2
            anchors.fill: parent
            color: "#F7F7FA"

            // 缩略页面 =====================================================================================
            Rectangle{
                id: tab1
                visible: flagtab === 1
                x:10*dp
                y:50*dp
                height: mainwindow_rect.height - 60*dp
                width: mainwindow_rect.width - 20*dp
                color: "#F7F7FA"

                Component.onCompleted: {
                    animationSimp.startAnimation()
                }

                Rectangle{      // 左侧 =========================================
                    id:lefttab
                    radius: 12
                    width: parent.width*0.15
                    height: parent.height-20*dp
                    Item{
                        height: parent.height
                        width: parent.width
                        Rectangle{
                            width: parent.width
                            height: parent.height
                            color: "white"
                            radius: 12
                            Column{
                                x: 10*dp
                                width: parent.width-20*dp
                                height: parent.height-20*dp
                                spacing: 10*dp
                                anchors.verticalCenter: parent.verticalCenter
                                Rectangle{
                                    height: parent.height*0.25
                                    width: parent.width
                                    color: "#FFF8F5"
                                    Column{
                                        y:10*dp
                                        spacing: 0*dp
                                        height: parent.height
                                        width: parent.width
                                        CircleButton{
                                            id: t2yunxing
                                            x: (parent.width-circleSize)/2
                                            y: parent.height*0.05
                                            title: "启动实验"
                                            icon: "qrc:/picture/j_yx.png"
                                            circleSize: isHovered?(parent.height/2>parent.width?parent.width*0.8:parent.height/2*0.8)*1.1:(parent.height/2>parent.width?parent.width*0.8:parent.height/2*0.8)
                                            onReleased: {
                                                if(flagss===0){
                                                    if(expConfigName.getText()===""||expInfoName.getText()==="")
                                                    {
                                                        boxerror_tab.show();
                                                    }
                                                    else if(!gConnectionState){
                                                        noConnectBox.show()
                                                    }
                                                    else if(expStatus!=0){

                                                    }
                                                    else
                                                    {
                                                        syqd1.show()
                                                    }
                                                }else{
                                                    sytz1.show()
                                                }
                                            }
                                        }
                                        CircleButton{
                                            id: t2zanting
                                            x: (parent.width-circleSize)/2
                                            y: parent.height/2+parent.height*0.05
                                            title: "暂停"
                                            icon: "qrc:/picture/j_zt.png"
                                            circleSize: isHovered?(parent.height/2>parent.width?parent.width*0.5:parent.height/2*0.5)*1.1:(parent.height/2>parent.width?parent.width*0.5:parent.height/2*0.5)
                                            onReleased: {
                                                if(flagcc===0){
                                                    syzt1.show()
                                                }else{
                                                    syjx1.show()
                                                }
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.12
                                    width: parent.width
                                    color: "#F7F6FF"
                                    Column{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        Rectangle{
                                            id:sj
                                            x:(parent.width-width)/2
                                            y:parent.height*0.3
                                            Text {
                                                id: t2sysj
                                                text: qsTr("剩余时间")
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pixelSize: config.pointsize + 2*dp
                                            }
                                        }
                                        Rectangle{
                                            x:(parent.width-width)/2
                                            y:parent.height*0.6
                                            Text {
                                                id: t2sysjval
                                                text: formatTime(endTime)
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.1
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.12
                                    width: parent.width
                                    color: "#F7F6FF"
                                    Column{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        Rectangle{
                                            id:ysj
                                            x:(parent.width-width)/2
                                            y:parent.height*0.3
                                            Text {
                                                id: t2yjssj
                                                text: qsTr("预结束时间")
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pixelSize: config.pointsize + 2*dp
                                            }
                                        }
                                        Rectangle{
                                            x:(parent.width-width)/2
                                            y:parent.height*0.6
                                            Text {
                                                id: t2yjssjval
                                                text: "00:00:00"
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.1
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.10
                                    width: parent.width
                                    color: "#F4FDF9"
                                    Row{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        Rectangle{
                                            id:bz
                                            x:parent.width*0.4
                                            y:parent.height*0.5
                                            Text {
                                                id: buzhou
                                                text: qsTr("步骤:")
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                            }
                                        }
                                        Rectangle{
                                            x:parent.width*0.6
                                            y:parent.height*0.5
                                            Text {
                                                id: buzhouval

                                                text:""
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.10
                                    width: parent.width
                                    color: "#F4FDF9"
                                    Row{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        Rectangle{
                                            id:tx
                                            x:parent.width*0.35
                                            y:parent.height*0.5
                                            Text {
                                                id: txzt
                                                text: qsTr("通信状态:")
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                            }
                                        }
                                        Rectangle{
                                            x:parent.width*0.7
                                            y:parent.height*0.5
                                            Text {
                                                id: txztval
                                                text: gConnectionState ? "已连接" : "未连接"
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                                color: gConnectionState ? "#56BD6C" :"red"
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.10
                                    width: parent.width
                                    color: "#F4FDF9"
                                    Row{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        Rectangle{
                                            id:yy
                                            x:parent.width*0.35
                                            y:parent.height*0.5
                                            Text {
                                                id: yykz
                                                text: qsTr("语音控制:")
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                            }
                                        }
                                        Rectangle{
                                            x:parent.width*0.7
                                            y:parent.height*0.5
                                            Text {
                                                id: yykzval
                                                text: gSpeechState ? "已连接" : "未连接"
                                                anchors.centerIn: parent // 让文字在该矩形中居中
                                                font.pointSize: lefttab.width*0.06
                                                color: gSpeechState ? "#56BD6C" :"red"
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.10
                                    width: parent.width
                                    Row{
                                        spacing: 10*dp
                                        height: parent.height
                                        width: parent.width
                                        anchors.horizontalCenter: parent.horizontalCenter // 水平居中
                                        PushButton{
                                            y:parent.height*0.3
                                            title: "打印报告"
                                            defaultMode2: true
                                            width: parent.width/2-10*dp
                                            onReleased: {
                                                dybg.show()
                                            }
                                        }
                                        PushButton{
                                            y:parent.height*0.3
                                            title: "实验信息"
                                            defaultMode2: true
                                            width: parent.width/2-10*dp
                                            onReleased: {
                                                box_info.setValue()
                                                box_info.show()
                                            }
                                        }
                                    }
                                }
                                Rectangle{
                                    height: parent.height*0.05
                                    width: parent.width
                                    PushButton{
                                        width: parent.width-20*dp
                                        y: parent.height*0.05
                                        anchors.horizontalCenter: parent.horizontalCenter
                                        title: "偏好设置"
                                        button_icon: "qrc:/picture/phsz.png"
                                        icon_height:20*dp
                                        icon_width:20*dp
                                        defaultMode1: true
                                        onReleased: {
                                            preset.show()
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
                Rectangle{
                    width: 0.85*parent.width-10*dp
                    height: parent.height-20*dp
                    x: 0.15*parent.width+10*dp
                    color: "white"
                    radius: 12
                    Item {
                        height: parent.height*0.75
                        width: parent.width
                    }
                }
                Item{       // 右侧 =========================================
                    width: 0.85*parent.width-10*dp
                    height: parent.height-20*dp
                    x: 0.15*parent.width+10*dp
                    Rectangle{
                        width: parent.width
                        height: parent.height
                        color: "#E0E6E9"
                        radius: 12
                        AnimationEquipmentBox{
                            id: animationSimp
                            anchors.fill: parent
                            showDetails: false
                            editable: expStatus===0 && gConnectionState
                        }

                        PushButton{
                            x:20*dp
                            y:parent.height-40*dp - 20*dp
                            z:1
                            title: "实验日志"
                            defaultMode2: true
                            onReleased: {
                                box_log.show()
                                var data=GetMessageInterface.getExperimentLog()
                                box_log.setData(data)
                            }
                        }
                    }
                }
            }

            // 详细页面 =====================================================================================
            Rectangle{
                id: tab0
                visible: flagtab === 0
                x:10*dp
                y:10*dp

                property bool isExpFullScreen: false    // 实验全屏
                property bool isShowExpInfo:   true    // 隐藏下面的几个东西
                property int centerShowMode:   0    // 0 实验和色谱图都显示 1 显示实验 2 显示色谱图

                height: mainwindow_rect.height - 20*dp
                width: mainwindow_rect.width - 20*dp
                color: "#F7F7FA"
                onVisibleChanged: {
                    sttime.setText(expDate)
                    expConsumblesType.setIndex(expConsumblesTypeIndex)
                    expConfigName.model=GetMessageInterface.getExperimentNameByConsumables(expConsumblesType.getText())
                    expConfigName.setIndex(expSettingIndex)
                }

                Component.onCompleted: {
                    // 启动动画
                    timer_startAnimation.start()
                }
                Timer{
                    id: timer_startAnimation
                    interval: 1000
                    onTriggered: {
                        console.log("动画模拟启动 ===== >>>")
                        animationLeft.startAnimation()
                        //                    animationRight.startAnimation()
                        timer_startAnimation.stop()
                    }

                }

                Rectangle{      // 顶部 =========================================
                    id: tab0_top_layout
                    radius: 12
                    width: parent.width
                    height: 150*dp

                    // 顶部-圆的按钮布局 ===============================================
                    Item{
                        height: parent.height
                        width: parent.width * 0.38
                        Row{
                            x: 40*dp
                            width: parent.width - 80*dp
                            spacing: (width - 50*dp*5 - 80*dp) / 5
                            anchors.verticalCenter: parent.verticalCenter
                            CircleButton{
                                id: yunxing
                                title: "运行"
                                icon: "qrc:/picture/x_yx.png"
                                circleSize: isHovered?1.1*80*dp:80*dp
                                width: 80*dp
                                height: 110*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expConfigName.getText()===""||expInfoName.getText()==="")
                                    {
                                        boxerror_tab.show();
                                    }
                                    else if(!gConnectionState){
                                        noConnectBox.show()
                                    }
                                    else if(expStatus!=0){

                                    }
                                    else
                                    {
                                        var data=CommunicationMethod.checkConsumablesSyring(expConsumblesType.getText());
                                        if(data>0){
                                            consumables_Err.show()
                                            return
                                        }
                                        else if(data===-1){
                                            noConsumables_Err.show()
                                            return
                                        }

                                        barCodeIsValid = CBarCode.isValidCode(edit_barCode.getText())       // 校验耗材编码好不好使
                                        //                                        if(!barCodeIsValid){
                                        //                                            mainwindow.showToast("耗材编码无效", 1)
                                        //                                            return
                                        //                                        }

                                        syqd1.show()
                                    }
                                }
                                AnimatedImage{
                                    id: yunxing_animated
                                    source: "qrc:/picture/j_yxz.gif"
                                    playing: true
                                    visible: false
                                    height: 94*dp; width: 94*dp
                                    y: -8*dp; x: -8*dp
                                }
                            }CircleButton{
                                id: tingzhi
                                title: "停止"
                                icon: "qrc:/picture/x_tz.png"
                                circleSize: isHovered?1.1*50*dp:50*dp
                                width: 50*dp
                                height: 80*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expStatus!=0){
                                        sytz1.show()
                                    }
                                }
                            }CircleButton{
                                id: zanting
                                title: "暂停"
                                icon: "qrc:/picture/x_zt.png"
                                circleSize: isHovered?1.1*50*dp:50*dp
                                width: 50*dp
                                height: 80*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expStatus==1){
                                        syzt1.show()
                                    }
                                }
                            }CircleButton{
                                id: jixu
                                title: "继续"
                                icon: "qrc:/picture/x_jx.png"
                                circleSize: isHovered?1.1*50*dp:50*dp
                                width: 50*dp
                                height: 80*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expStatus==2){
                                        syjx1.show()
                                    }
                                }
                            }CircleButton{
                                id: fuwei
                                title: "复位"
                                icon: "qrc:/picture/x_fw.png"
                                circleSize: isHovered?1.1*50*dp:50*dp
                                width: 50*dp
                                height: 80*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expStatus!=1){
                                        syfw1.show()
                                    }else{
                                        resetWarning.show()
                                    }
                                }
                                CustomTooltip{
                                    visible: parent.isHovered
                                    text: "复位到实验初始位置"
                                }
                            }CircleButton{
                                id: jiting
                                title: "急停"
                                visible: false
                                icon: "qrc:/picture/x_jt.png"
                                circleSize: isHovered?1.1*50*dp:50*dp
                                width: 50*dp
                                height: 80*dp
                                anchors.verticalCenter: parent.verticalCenter
                                onReleased: {
                                    if(expStatus==1){
                                        syjt1.show()
                                    }
                                }
                            }
                        }
                    }
                    // 顶部-时间布局 =============================================
                    Item{
                        id: layout_time
                        height: parent.height
                        x: parent.width * 0.39
                        width: parent.width * 0.48
                        Row{
                            x: 40*dp
                            width: parent.width - 80*dp
                            spacing: (width-120*dp*5-60*dp) / 4
                            anchors.verticalCenter: parent.verticalCenter
                            DoubleRectangle{
                                id:sysjva
                                width: 140*dp
                                height: 95*dp
                                text1: "剩余时间"
                                text2: formatTime(endTime)
                                upcolor: "#F7F6FF"
                                iconpath: "qrc:/picture/zy_sysj.png"
                            }DoubleRectangle{
                                id:finishDate
                                width: 160*dp
                                height: 95*dp
                                text1: "预结束时间"
                                text2: "00:00:00"
                                upcolor: "#F5F9FF"
                                iconpath: "qrc:/picture/zy_ysysj.png"
                            }DoubleRectangle{
                                id:expStep
                                width: 140*dp
                                height: 95*dp
                                text1: "步骤"
                                text2: ""
                                upcolor: "#F4FDF9"
                                iconpath: "qrc:/picture/zy_bz.png"
                            }DoubleRectangle{
                                id: connectStatus
                                width: 140*dp
                                height: 95*dp
                                text1: "通信状态"
                                text2: gConnectionState ? "已连接" : "未连接"
                                upcolor: "#FFF7F5"
                                t2color: gConnectionState ? "#56BD6C" :"red"
                                iconpath: "qrc:/picture/zy_tx.png"
                                onChangePresetView: {
                                    preset.show()
                                    preset.curPage=3
                                }
                            }DoubleRectangle{
                                width: 140*dp
                                height: 95*dp
                                text1: "语音控制"
                                text2: gSpeechState ? "已连接" : "未连接"
                                upcolor: "#FFF7F7"
                                t2color: gSpeechState ? "#56BD6C" :"red"
                                iconpath: "qrc:/picture/zy_yy.png"
                                onChangePresetView: {
                                    preset.show()
                                    preset.curPage=7
                                }
                            }
                        }
                    }
                    // 顶部-方的按钮布局
                    Item{
                        height: 150*dp
                        x: parent.width * 0.9
                        width: parent.width * 0.1
                        PushButton{
                            id:dayinbaogao
                            title: "打印报告"
                            defaultMode1: true
                            y: parent.height / 2 - 45*dp
                            anchors.horizontalCenter: parent.horizontalCenter
                            icon_height:16*dp
                            icon_width:16*dp
                            button_icon:"qrc:/picture/x_dybg.png"
                            onReleased: {
                                dybg.show()
                            }
                        }

                        PushButton{
                            id:phsz
                            width: 120*dp
                            y: parent.height / 2 + 5*dp
                            anchors.horizontalCenter: parent.horizontalCenter
                            title: "偏好设置"
                            button_icon: "qrc:/picture/phsz.png"
                            icon_height:20*dp
                            icon_width:20*dp
                            defaultMode1: true
                            onReleased: {
                                preset.show()
                            }
                        }
                    }

                }

                // 中间的两个图
                Item{
                    id: tab0_center_layout
                    width: parent.width
                    height: tab0.isExpFullScreen ? parent.height : (tab0.isShowExpInfo ? parent.height - 560*dp : parent.height - y)
                    y: tab0.isExpFullScreen ? 0 : (tab0_top_layout.height+50*dp)
                    z: 1
                    AnimationChromatogramBox{
                        id: animationRight
                        visible: tab0.centerShowMode !== 1
                        width: tab0.centerShowMode===2 ? parent.width : 0.35*parent.width
                        height: parent.height
                        x: tab0.centerShowMode===2 ? 0 : 0.65*parent.width
                    }

                    AnimationEquipmentBox{
                        id: animationLeft
                        visible: tab0.centerShowMode !== 2
                        width: (tab0.isExpFullScreen || tab0.centerShowMode===1) ? parent.width : 0.65*parent.width - 10*dp
                        height: parent.height
                        showDetails: true
                        editable: expStatus===0 && gConnectionState
                        //                        editable: true
                        onFullScreen: {
                            tab0.isExpFullScreen = !tab0.isExpFullScreen
                        }
                    }
                    // 按钮 - 放大实验
                    Image{
                        id: button_largeExp
                        height: 60*dp;  width: 10*dp
                        source: "qrc:/picture/rect_right_arrow.png"
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.verticalCenterOffset: tab0.centerShowMode===0 ? -34*dp : 0
                        visible: tab0.centerShowMode !== 1 && ! tab0.isExpFullScreen
                        x: tab0.centerShowMode===2 ? 0 : animationLeft.x+animationLeft.width
                        z: 1
                        opacity: isHovered ? 0.8: 1
                        property bool isHovered: false
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: "PointingHandCursor"
                            hoverEnabled: true
                            onEntered: { parent.isHovered = true }
                            onExited: { parent.isHovered = false }
                            onClicked: {
                                if(tab0.centerShowMode===0){
                                    tab0.centerShowMode = 1
                                }else if(tab0.centerShowMode===2){
                                    tab0.centerShowMode = 0
                                }
                            }
                        }
                        CustomTooltip{
                            visible: parent.isHovered
                            text: "放大"
                        }
                    }
                    // 按钮 - 放大色谱图
                    Image{
                        id: button_largeChrom
                        visible: tab0.centerShowMode !== 2 && ! tab0.isExpFullScreen
                        height: 60*dp;  width: 10*dp
                        source: "qrc:/picture/rect_left_arrow.png"
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.verticalCenterOffset: tab0.centerShowMode===0 ? 34*dp : 0
                        x: animationLeft.x+animationLeft.width + (tab0.centerShowMode===1?-width:0)
                        z: 1
                        opacity: isHovered ? 0.8: 1
                        property bool isHovered: false
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: "PointingHandCursor"
                            hoverEnabled: true
                            onEntered: { parent.isHovered = true }
                            onExited: { parent.isHovered = false }
                            onClicked: {
                                if(tab0.centerShowMode===0){
                                    tab0.centerShowMode = 2
                                }else if(tab0.centerShowMode===1){
                                    tab0.centerShowMode = 0
                                }
                            }
                        }
                        CustomTooltip{
                            visible: parent.isHovered
                            text: "放大"
                        }
                    }
                }

                // 按钮
                Image{
                    id: button_showExpInfo
                    visible: ! tab0.isExpFullScreen
                    height: 10*dp;  width: 70*dp
                    source: tab0.isShowExpInfo ? "qrc:/picture/rect_down_arrow.png" : "qrc:/picture/rect_up_arrow.png"
                    y: tab0_center_layout.y + tab0_center_layout.height - (tab0.isShowExpInfo?0:10*dp)
                    anchors.horizontalCenter: parent.horizontalCenter
                    z: 1
                    opacity: isHovered ? 0.8: 1
                    property bool isHovered: false
                    MouseArea{
                        anchors.fill: parent
                        cursorShape: "PointingHandCursor"
                        hoverEnabled: true
                        onEntered: { parent.isHovered = true }
                        onExited: { parent.isHovered = false }
                        onClicked: {
                            tab0.isShowExpInfo = !tab0.isShowExpInfo
                        }
                    }
                    CustomTooltip{
                        visible: parent.isHovered
                        text: "放大"
                    }
                }

                // 底下的东西
                Item{
                    id: tab0_bottom_layout
                    visible: tab0.isShowExpInfo
                    width: parent.width
                    height: 350*dp
                    y: parent.height - 350*dp
                    Rectangle{
                        width: parent.width * 0.4 - 10*dp
                        height: parent.height
                        radius: 12
                        Column{
                            x: 20*dp; y: 10*dp
                            height: parent.height - 30*dp
                            width: parent.width - 40*dp
                            spacing: (height-40*dp*6) / 5
                            Rectangle{
                                height: 40*dp; width: parent.width
                                Text {
                                    anchors.verticalCenter: parent.verticalCenter
                                    text: qsTr("实验信息")
                                    font.bold: true
                                    font.pixelSize: config.fontsize+5*dp
                                }
                            }
                            Row{
                                height: 40*dp
                                width: parent.width
                                spacing: 20*dp

                                CustomComboBox {
                                    id:expConsumblesType
                                    editWidth: parent.width / 2 - 80*dp - 10*dp
                                    title: "耗材名称"
                                    model:GetMessageInterface.getConsumablesName()
                                    onCurrentText: {
                                        expConfigName.model=GetMessageInterface.getExperimentNameByConsumables(text)
                                        animationLeft.refreshConsumable(getText())
                                    }
                                    CustomTooltip{
                                        visible: parent.isHovered
                                        text: "更换耗材后需要点击确认按钮"
                                    }
                                }

                                CustomComboBox {
                                    id:expConfigName
                                    editWidth: parent.width / 2 - 80*dp - 10*dp-110*dp
                                    title: "实验名称"
                                    model: GetMessageInterface.getExperimentNameByConsumables(expConsumblesType.getText())
                                    onCurrentText: {
                                        var logData= GetMessageInterface.getExperimentLog(expConsumblesType.getText(),text)
                                        experimentLog.setData(logData)
                                        var otherData= GetMessageInterface.getOtherExperimentInfo();
                                        expStep.text2=otherData["StepInfo"]
                                        buzhouval.text=qsTr(otherData["StepInfo"])
                                        CAnimation.updateColor(getText())
                                    }
                                }

                                CustomButton{
                                    title: "确认"
                                    width: 90*dp
                                    defaultMode1: true
                                    onReleased:
                                    {
                                        expConsumblesTypeName=expConsumblesType.getText()
                                        expConsumblesTypeIndex=expConsumblesType.getIndex()
                                        expSettingName=expConfigName.getText()
                                        expSettingIndex=expConfigName.getIndex()
                                        CommunicationMethod.setConsumable(expConsumblesType.getText());
                                        CAnimation.reset()      // 动画复位
                                    }
                                }
                            }

                            Row{
                                height: 40*dp
                                width: parent.width
                                spacing: 20*dp
                                CustomInputText{
                                    id:expInfoName
                                    editWidth: parent.width / 2 - 80*dp - 10*dp
                                    title: "名       称"
                                    defaultText: name
                                    inputMaxLength: 30
                                    onTextChanged: {
                                        name=getText()
                                    }
                                }CustomInputText{
                                    editable: false
                                    editWidth: parent.width / 2 - 80*dp - 10*dp
                                    title: "设备名称"
                                    defaultText: "BK-RadPharm-001"
                                }
                            }

                            Row{
                                height: 40*dp
                                spacing: 20*dp
                                width: parent.width
                                Text{
                                    anchors.verticalCenter: parent.verticalCenter
                                    text: "实验日期"
                                    font.pixelSize: config.fontsize
                                    width: 60*dp
                                }
                                CustomComboCalendar{
                                    id: sttime
                                    editWidth: parent.width / 2 - 80*dp - 10*dp
                                    date: expDate
                                    onDateChanged: {
                                        expDate=date
                                        console.log(expDate)       // 字符串格式 "2024-12-18"
                                    }
                                }CustomInputText{
                                    id: edit_operator
                                    editWidth: parent.width / 2 - 80*dp - 10*dp
                                    title: "操  作  人"
                                    defaultText: operator
                                    inputMaxLength: 30
                                    onTextChanged: {
                                        operator=getText()
                                    }
                                }
                                z:1
                            }

                            CustomInputText{
                                id: edit_barCode
                                editWidth: parent.width / 2 - 80*dp - 10*dp
                                title: "耗材编码"
                                defaultText: barCode
                                onTextChanged: {
                                    barCode=getText()
                                }

                                onEnterPressed: {       // 检查条码是否好用
                                    barCodeIsValid = CBarCode.addBarCode(getText())
                                    if(barCodeIsValid){
                                        mainwindow.showToast("耗材编码录入成功", 0)
                                    }else{
                                        mainwindow.showToast("耗材编码录入失败", 1)
                                    }
                                }

                                CustomButton{
                                    width: 60*dp
                                    title: barCodeIsValid ? "有效" : "无效"
                                    text_color: barCodeIsValid ? "green" : "red"
                                    text_color_hover: barCodeIsValid ? "#44FF44" : "#FF4444"
                                    text_color_press: barCodeIsValid ? "#45CA65" : "#FF4444"
                                    x: parent.width - width
                                    button_color: "transparent"
                                    onClicked: {

                                    }
                                }
                            }

                            BoxBarCode{
                                id: box_barcode
                            }

                            CustomInputText{
                                id: edit_memo
                                editWidth: parent.width - 80*dp
                                title: "描       述"
                                defaultText: memo
                                inputMaxLength: 50
                                onTextChanged: {
                                    memo=getText()
                                }
                            }
                        }
                    }

                    Rectangle{
                        width: parent.width * 0.6
                        height: parent.height
                        x: parent.width * 0.4
                        radius: 12
                        ExperimentLog{
                            id: experimentLog
                        }
                    }

                }
            }


            // 信息条 ----->   (缩略页面和详细页面的信息条用一个了)
            Rectangle{
                color: "#FDF6EC"
                height: 35*dp
                width: parent.width - 20*dp
                visible: !(flagtab===0 && tab0.isExpFullScreen)
                y: flagtab === 0 ? (layout_time.y + layout_time.height + 17.5*dp) : 7.5*dp
                x: 10*dp
                Image{
                    source: "qrc:/picture/warn.png"
                    height: 18*dp; width: 18*dp
                    anchors.verticalCenter: parent.verticalCenter
                    x: 30*dp
                }
                Row{
                    id: layout_expProInfo
                    x: 80*dp
                    property var infoTitle: ["芯片耗材", "压紧", "压力值", "能谱检测", "废液瓶", "产物瓶"]
                    property var infoValue: [ "无", "是", "0Mpa","无", "无", "无"]
                    anchors.verticalCenter: parent.verticalCenter
                    Repeater{
                        model: layout_expProInfo.infoTitle.length
                        Row{
                            Text {
                                text: layout_expProInfo.infoTitle[index] + "   ( "
                                font.pixelSize: config.fontsize
                            }
                            Text{
                                text: layout_expProInfo.infoValue[index]
                                color: (text==="无"||text==="否") ? "red" : "green"
                                font.pixelSize: config.fontsize
                            }Text{
                                text: " )      |      "
                                font.pixelSize: config.fontsize
                            }
                        }
                    }
                    Connections{
                        target: CSignal
                        onMaintenanceInfoChanged: {
                            console.log("收到设备状态变化", type, value)
                            if(type==="芯片耗材"){
                                layout_expProInfo.infoValue[0] = value
                            }if(type==="压紧"){
                                layout_expProInfo.infoValue[1] = value
                            }if(type==="压力值"){
                                layout_expProInfo.infoValue[2] = value
                            }if(type==="能谱检测"){
                                layout_expProInfo.infoValue[3] = value
                            }if(type==="废液瓶"){
                                layout_expProInfo.infoValue[4] = value
                            }if(type==="产物瓶"){
                                layout_expProInfo.infoValue[5] = value
                            }
                            layout_expProInfo.infoValueChanged()
                        }
                    }
                }
            }

            BoxLicense{
                id: license
                z: 100
            }
        }
    }

    Connections{
        target: CSignal
        onCommunicationStatus:{     // 通信状态
            gConnectionState = (status===0)
        }
        onVoiceAssistant:{
            gSpeechState=(status===0)
        }

        onRefExperimentLog:{
            var data = GetMessageInterface.getExperimentLog();
            experimentLog.setData(data)
            var otherData= GetMessageInterface.getOtherExperimentInfo();
            expStep.text2=otherData["StepInfo"]
            buzhouval.text=qsTr(otherData["StepInfo"])
        }
        onExperimentStatus:{
            //01 启动实验 02停止,03暂停,04继续,05复位,06急停
            if(type=="0")//试验结束
            {
                expStatus=0
                expFinishing.show()
                yunxing_animated.visible = false

                updateReportInfo()
                CReportManager.exportReport();

                CAnimation.reset()                  // 动画复位
                CChromManager.onEndExperiment()     // 关闭能谱图数据获取
                reSetMsg()
            }

            if(type=="1")
            {
                if(status==0){
                    flagss=1
                    expStatus=1
                    yunxing_animated.visible = true        // 运行按钮动画状态效果切换
                    var otherData= GetMessageInterface.getOtherExperimentInfo();
                    expStep.text2=otherData["StepInfo"]
                    buzhouval.text=qsTr(otherData["StepInfo"])
                    flagcc=0

                    startTime = new Date().getTime();
                    endTime = otherData["ExpTime"]
                    t2yjssjval.text=new Date(new Date().getTime() +1000*endTime).toLocaleTimeString()
                    finishDate.text2=new Date(new Date().getTime() +1000*endTime).toLocaleTimeString()
                    timerRunning=true


                    t2yunxing.icon="qrc:/picture/j_tz.png"
                    t2yunxing.title="停止"
                    CChromManager.onStartExperiment()     // 开启能谱图数据获取

                }
            }

            if(type=="2")//停止
            {
                if(status==0){
                    if(expStatus!==0){
                        expFinishing.show()
                    }

                    expStatus=0
                    yunxing_animated.visible = false
                    t2yunxing.icon="qrc:/picture/j_yx.png"
                    t2yunxing.title="启动实验"
                    flagss=0
                    timerRunning = false;
                    CAnimation.reset()                  // 动画复位
                    CChromManager.onEndExperiment()     // 关闭能谱图数据获取
                    updateReportInfo()
                    CReportManager.exportReport();
                    reSetMsg()
                }
            }

            if(type=="3")//暂停
            {
                if(status==0){
                    flagss=1
                    expStatus=2
                    timerRunning=false
                    yunxing_animated.visible = false        // 运行按钮动画状态效果切换
                    flagcc=1
                    t2zanting.icon="qrc:/picture/j_jx.png"
                    t2zanting.title="继续"
                }
            }

            if(type=="4")//继续
            {
                if(status==0){
                    flagss=1
                    expStatus=1
                    timerRunning=true
                    yunxing_animated.visible = true;       // 运行按钮动画状态效果切换
                    flagcc=0
                    t2yjssjval.text=new Date(new Date().getTime() +1000*endTime).toLocaleTimeString()
                    finishDate.text2=new Date(new Date().getTime() +1000*endTime).toLocaleTimeString()

                    t2zanting.icon="qrc:/picture/j_zt.png"
                    t2zanting.title="暂停"
                }
            }
            if(type=="6")//急停
            {
                if(status==0){
                    yunxing_animated.visible = false;       // 运行按钮动画状态效果切换
                    expStatus=0
                    reSetMsg()
                    CAnimation.reset()                  // 动画复位
                    CChromManager.onEndExperiment()     // 关闭能谱图数据获取
                    updateReportInfo()
                    CReportManager.exportReport();
                }
            }

            if(type=="7"){
                if(status==0)
                    net_Err.show()
            }
        }
        onSendErrorMsg:{
            errorWind.error_text=errorInfo
            errorWind.method_text=methodInfo
            errorWind.show()
        }
        onSendResetMsg:{
            if(status==="0"){
                if(serial==="7"){
                    toast.show("转盘复位完成",0)
                }
                else{
                    toast.show(serial+"号注射器复位完成",0)
                }
            }
            else if(status==="1"){
                if(serial==="7"){
                    toast.show("转盘正在运动中,请耐心等待",1)
                }
                else if(serial==="0")
                {
                    toast.show("有电机正在运动中,请耐心等待",1)
                }
                else{
                    toast.show(serial+"号注射器正在运动中,请耐心等待",1)
                }
            }
        }
    }

    function reSetMsg(){
        endTime=0
        name=Qt.formatDateTime(new Date, "yyyyMMddhhmmss")
        expDate=Qt.formatDateTime(new Date, "yyyy-MM-dd")
        sttime.setText(expDate)
        operator=""
        //        barCode=""
        memo= ""
        t2yjssjval.text="00:00:00"
        finishDate.text2="00:00:00"

        expConsumblesType.model=GetMessageInterface.getConsumablesName()
        var data=GetMessageInterface.getDefaultExeConfig()
        expConsumblesType.setText(data[0])
        expConfigName.model=GetMessageInterface.getExperimentNameByConsumables(expConsumblesType.getText())
        expConfigName.setText(data[1])

        var logData = GetMessageInterface.getExperimentLog(expConsumblesType.getText(),expConfigName.getText());
        experimentLog.setData(logData)

        var otherData= GetMessageInterface.getOtherExperimentInfo();
        expStep.text2=otherData["StepInfo"]
        buzhouval.text=qsTr(otherData["StepInfo"])
        t2yunxing.icon="qrc:/picture/j_yx.png"
        t2yunxing.title="启动实验"
        flagss=0
    }

    BoxErrorInfo{
        id:errorWind
    }

    BoxWarning{
        id:resetWarning
        box_text: "实验过程中禁止复位"
    }

    BoxExperimentFinish{
        id:expFinishing
    }

    BoxWarning{
        id: syqd1
        box_text: "确定开启实验吗?"
        onAccept: {
            CommunicationMethod.startExperiment();
        }
    }

    BoxWarning{
        id: sytz1
        box_text: "确定停止实验吗?\n(运行完当前步骤后停止实验,无法继续实验)"
        onAccept: {
            CommunicationMethod.stopExperiment(2);
            CAnimation.reset()
        }
    }

    BoxWarning{
        id: syzt1
        box_text: "确定暂停实验吗?\n(运行完当前步骤后停止实验,可以继续实验)"
        onAccept: {
            CommunicationMethod.stopExperiment(3);
        }
    }

    BoxWarning{
        id: syjx1
        box_text: "确定继续实验吗?"
        onAccept: {
            CommunicationMethod.stopExperiment(4);
        }
    }

    BoxWarning{
        id: syfw1
        box_text: "请确认耗材是否取出,确定复位吗?\n(执行设备机械复位,恢复初始状态)"
        onAccept: {
            CMaintenance.reset("1","0",expConsumblesType.getText())
            CAnimation.reset()
        }
    }

    BoxWarning{
        id: syjt1
        box_text: "确定立即停止设备吗?\n(立刻停止设备运行状态)"
        onAccept: {
            CommunicationMethod.stopExperiment(6);
        }
    }

    BoxWarning{
        id: dybg
        box_text: "是否需要打印报告?"
        onAccept: {
            updateReportInfo()
            box_printer.show()
        }
    }

    BoxWarning{
        id: xtpz
        box_text: "是否需要系统配置?"
        onAccept: {

        }
    }

    BoxWarning{
        id:noConnectBox
        box_text: "未连接设备"
    }

    BoxError{
        id: boxerror_tab
        box_text: "请填写完整的实验信息"
    }

    BoxError{
        id: net_Err
        box_text: "网络通信异常"
    }
    BoxError{
        id: noConsumables_Err
        box_text: "未检测到放置耗材,无法启动实验"
    }
    BoxError{
        id: consumables_Err
        box_text: "当前耗材注射器参数配置不全,无法启动实验"
    }

    BoxExperimentLog{
        id: box_log
    }

    BoxPrinter{
        id: box_printer
    }

    BoxExperimentInfo{
        id:box_info
    }

    PreferenceSettings{
        id:preset
        onClosing:{
            if(expStatus==0){
                expConsumblesType.model=GetMessageInterface.getConsumablesName()
                var data=GetMessageInterface.getDefaultExeConfig()
                expConsumblesType.setText(data[0])
                expConfigName.model=GetMessageInterface.getExperimentNameByConsumables(expConsumblesType.getText())
                expConfigName.setText(data[1])

                var logData = GetMessageInterface.getExperimentLog(expConsumblesType.getText(),expConfigName.getText());
                experimentLog.setData(logData)

                var otherData= GetMessageInterface.getOtherExperimentInfo();
                expStep.text2=otherData["StepInfo"]
                buzhouval.text=qsTr(otherData["StepInfo"])

                CAnimation.updateColor(expConfigName.getText())
            }
        }
    }

    function showToast(msg, type){
        toast.show(msg, type)
    }

    Toast{
        id: toast
        x: (parent.width-width) * 0.5
        y: 200*dp
    }
}
